#include "response_request.h"
#include "pakge_response_json.h"

#define RESPONSE_MSG_MAX_LEN        (1024)  /* 响应GET请求的消息长度 */

/* 处理http接收的数据 */
static error_e recv_http_data_hendler(httpd_req_t *req, unsigned char *recvbuf, unsigned int len)
{
    int ret = 0;
    memset_s(recvbuf, len, 0, len);

    ret = httpd_req_recv(req, recvbuf, len);
    if (ret <= 0)
    {
        if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
            /* 如果是超时，可以调用 cs_httpd_req_recv() 重试
             * 简单起见，这里我们直接
             * 响应 HTTP 408（请求超时）错误给客户端 */
            httpd_resp_send_408(req);
        }
        httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "fail");
        return STO_REQUEST_FAIL; 
    }
    
    return STO_SUCCESS;
}

/* 响应登录界面 */
int response_post_login(httpd_req_t *req)
{
    int cmd = CMD_LOGIN_CHECK;

    size_t buf_len = req->content_len;
    unsigned char buf[RESPONSE_MSG_MAX_LEN] = {0};
    if(buf_len > RESPONSE_MSG_MAX_LEN)
    {
        storage_debug("recv data > RESPONSE_MSG_MAX_LEN\r\n");
    }

    error_e ret = recv_http_data_hendler(req, buf, buf_len);
    if(ret != STO_SUCCESS)
    {
        storage_debug("http recv error\r\n");
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    ret = parse_login_json(buf);
    if(ret != STO_SUCCESS)
    {
        storage_debug("send: %s\n",default_reply_fail_msg(cmd, ret));
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    return httpd_resp_send(req, default_reply_succedd_msg(cmd), strlen(default_reply_succedd_msg(cmd)));  
}

/* 设置登录密码 */
int response_post_set_login(httpd_req_t *req)
{
    int cmd = CMD_SET_LOGIN_CONFIG;

    size_t buf_len = req->content_len;
    unsigned char buf[RESPONSE_MSG_MAX_LEN] = {0};
    if(buf_len > RESPONSE_MSG_MAX_LEN)
    {
        storage_debug("recv data > RESPONSE_MSG_MAX_LEN\r\n");
    }

    error_e ret = recv_http_data_hendler(req, buf, buf_len);
    if(ret != STO_SUCCESS)
    {
        storage_debug("http recv error\r\n");
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    ret = parse_set_login_json(buf);
    if(ret != STO_SUCCESS)
    {
        storage_debug("send: %s\n",default_reply_fail_msg(cmd, ret));
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    return httpd_resp_send(req, default_reply_succedd_msg(cmd), strlen(default_reply_succedd_msg(cmd)));  
}

/* 响应串口配置信息获取 */
int response_get_uart_config(httpd_req_t *req)
{
    char json_Response[RESPONSE_MSG_MAX_LEN] = {0};
    httpd_resp_set_hdr(req,  "Access-Control-Allow-Origin", "*");       //跨域传输协议
    httpd_resp_set_type(req, "application/json");                       // 设置http响应类型
  
    if(pakge_uart_config_json(json_Response, RESPONSE_MSG_MAX_LEN) != STO_SUCCESS){
        httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "fail");
        storage_debug("get uart and dido config fail.\n");
        return STO_REQUEST_FAIL;
    }

    return httpd_resp_send(req, json_Response, strlen(json_Response));  //发送一个完整的HTTP响应。内容在json_response中
}

/* 响应串口配置信息设置 */
int response_post_uart_config(httpd_req_t *req)
{
    int cmd = CMD_POST_UART_CONFIG;

    size_t buf_len = req->content_len;
    unsigned char buf[RESPONSE_MSG_MAX_LEN] = {0};
    if(buf_len > RESPONSE_MSG_MAX_LEN)
    {
        storage_debug("recv data > RESPONSE_MSG_MAX_LEN\r\n");
    }

    error_e ret = recv_http_data_hendler(req, buf, buf_len);
    if(ret != STO_SUCCESS)
    {
        storage_debug("http recv error\r\n");
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    ret = parse_uart_config_json(buf);
    if(ret != STO_SUCCESS)
    {
        storage_debug("send: %s\n",default_reply_fail_msg(cmd, ret));
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    return httpd_resp_send(req, default_reply_succedd_msg(cmd), strlen(default_reply_succedd_msg(cmd)));  
}

/* 响应网络信息获取 */
int response_get_net_info(httpd_req_t *req)
{
    char json_Response[RESPONSE_MSG_MAX_LEN] = {0};
    httpd_resp_set_hdr(req,  "Access-Control-Allow-Origin", "*");       //跨域传输协议
    httpd_resp_set_type(req, "application/json");                       // 设置http响应类型
  
    if(pakge_net_info_json(json_Response, RESPONSE_MSG_MAX_LEN) != STO_SUCCESS){
        httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "fail");
        storage_debug("get uart and dido config fail.\n");
        return STO_REQUEST_FAIL;
    }

    return httpd_resp_send(req, json_Response, strlen(json_Response));  //发送一个完整的HTTP响应。内容在json_response中    
}

/* 响应网络配置信息获取 */
int response_get_net_config(httpd_req_t *req)
{
    char json_Response[RESPONSE_MSG_MAX_LEN] = {0};
    httpd_resp_set_hdr(req,  "Access-Control-Allow-Origin", "*");       //跨域传输协议
    httpd_resp_set_type(req, "application/json");                       // 设置http响应类型
  
    if(pakge_net_config_json(json_Response, RESPONSE_MSG_MAX_LEN) != STO_SUCCESS){
        httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "fail");
        storage_debug("get uart and dido config fail.\n");
        return STO_REQUEST_FAIL;
    }

    return httpd_resp_send(req, json_Response, strlen(json_Response));  //发送一个完整的HTTP响应。内容在json_response中    
}

/* 响应网络配置信息设置 */
int response_post_net_config(httpd_req_t *req)
{
    int cmd = CMD_POST_NETWORK_CONFIG;

    size_t buf_len = req->content_len;
    unsigned char buf[RESPONSE_MSG_MAX_LEN] = {0};
    if(buf_len > RESPONSE_MSG_MAX_LEN)
    {
        storage_debug("recv data > RESPONSE_MSG_MAX_LEN\r\n");
    }

    error_e ret = recv_http_data_hendler(req, buf, buf_len);
    if(ret != STO_SUCCESS)
    {
        storage_debug("http recv error\r\n");
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    ret = parse_net_config_json(buf);
    if(ret != STO_SUCCESS)
    {
        storage_debug("send: %s\n",default_reply_fail_msg(cmd, ret));
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    return httpd_resp_send(req, default_reply_succedd_msg(cmd), strlen(default_reply_succedd_msg(cmd)));  
}

/* 响应凭证配置信息获取 */
int response_get_voucher_config(httpd_req_t *req)
{
    char json_Response[RESPONSE_MSG_MAX_LEN] = {0};
    httpd_resp_set_hdr(req,  "Access-Control-Allow-Origin", "*");       //跨域传输协议
    httpd_resp_set_type(req, "application/json");                       // 设置http响应类型
  
    if(pakge_voucher_config_json(json_Response, RESPONSE_MSG_MAX_LEN) != STO_SUCCESS){
        httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "fail");
        storage_debug("get uart and dido config fail.\n");
        return STO_REQUEST_FAIL;
    }

    return httpd_resp_send(req, json_Response, strlen(json_Response));  //发送一个完整的HTTP响应。内容在json_response中    
}

/* 响应凭证配置信息设置 */
int response_post_voucher_config(httpd_req_t *req)
{
    int cmd = CMD_POST_VOUCHER_CONFIG;

    size_t buf_len = req->content_len;
    unsigned char buf[RESPONSE_MSG_MAX_LEN] = {0};
    if(buf_len > RESPONSE_MSG_MAX_LEN)
    {
        storage_debug("recv data > RESPONSE_MSG_MAX_LEN\r\n");
    }

    error_e ret = recv_http_data_hendler(req, buf, buf_len);
    if(ret != STO_SUCCESS)
    {
        storage_debug("http recv error\r\n");
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    ret = parse_voucher_config_json(buf);
    if(ret != STO_SUCCESS)
    {
        storage_debug("send: %s\n",default_reply_fail_msg(cmd, ret));
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    return httpd_resp_send(req, default_reply_succedd_msg(cmd), strlen(default_reply_succedd_msg(cmd)));  
}

/* 响应ntp配置信息获取 */
int response_get_ntp_config(httpd_req_t *req)
{
    char json_Response[RESPONSE_MSG_MAX_LEN] = {0};
    httpd_resp_set_hdr(req,  "Access-Control-Allow-Origin", "*");       //跨域传输协议
    httpd_resp_set_type(req, "application/json");                       // 设置http响应类型
  
    if(pakge_ntp_config_json(json_Response, RESPONSE_MSG_MAX_LEN) != STO_SUCCESS){
        httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "fail");
        storage_debug("get uart and dido config fail.\n");
        return STO_REQUEST_FAIL;
    }

    return httpd_resp_send(req, json_Response, strlen(json_Response));  //发送一个完整的HTTP响应。内容在json_response中    
}

/* 响应ntp配置信息设置 */
int response_post_ntp_config(httpd_req_t *req)
{
    int cmd = CMD_POST_NTP_SERVER_CONFIG;

    size_t buf_len = req->content_len;
    unsigned char buf[RESPONSE_MSG_MAX_LEN] = {0};
    if(buf_len > RESPONSE_MSG_MAX_LEN)
    {
        storage_debug("recv data > RESPONSE_MSG_MAX_LEN\r\n");
    }

    error_e ret = recv_http_data_hendler(req, buf, buf_len);
    if(ret != STO_SUCCESS)
    {
        storage_debug("http recv error\r\n");
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    ret = parse_ntp_config_json(buf);
    if(ret != STO_SUCCESS)
    {
        storage_debug("send: %s\n",default_reply_fail_msg(cmd, ret));
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    return httpd_resp_send(req, default_reply_succedd_msg(cmd), strlen(default_reply_succedd_msg(cmd)));  
}

/* 响应ntp配置信息获取 */
int response_get_ota_config(httpd_req_t *req)
{
    char json_Response[RESPONSE_MSG_MAX_LEN] = {0};
    httpd_resp_set_hdr(req,  "Access-Control-Allow-Origin", "*");       //跨域传输协议
    httpd_resp_set_type(req, "application/json");                       // 设置http响应类型
  
    if(pakge_ota_config_json(json_Response, RESPONSE_MSG_MAX_LEN) != STO_SUCCESS){
        httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "fail");
        storage_debug("get uart and dido config fail.\n");
        return STO_REQUEST_FAIL;
    }

    return httpd_resp_send(req, json_Response, strlen(json_Response));  //发送一个完整的HTTP响应。内容在json_response中    
}

/* 响应ntp配置信息设置 */
int response_post_ota_config(httpd_req_t *req)
{
    int cmd = CMD_POST_OTA_SERVER_CONFIG;

    size_t buf_len = req->content_len;
    unsigned char buf[RESPONSE_MSG_MAX_LEN] = {0};
    if(buf_len > RESPONSE_MSG_MAX_LEN)
    {
        storage_debug("recv data > RESPONSE_MSG_MAX_LEN\r\n");
    }

    error_e ret = recv_http_data_hendler(req, buf, buf_len);
    if(ret != STO_SUCCESS)
    {
        storage_debug("http recv error\r\n");
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    ret = parse_ota_config_json(buf);
    if(ret != STO_SUCCESS)
    {
        storage_debug("send: %s\n",default_reply_fail_msg(cmd, ret));
        return httpd_resp_send(req, default_reply_fail_msg(cmd, ret), strlen(default_reply_fail_msg(cmd, ret)));   
    }

    return httpd_resp_send(req, default_reply_succedd_msg(cmd), strlen(default_reply_succedd_msg(cmd)));  
}
